from typing import List, Optional, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field


# 小红书账号相关模式
class XiaohongshuAccountBase(BaseModel):
    """小红书账号基础模式"""
    nickname: str = Field(..., description="昵称")
    account_name: Optional[str] = Field(None, description="账号名")
    followers: str = Field("0", description="粉丝数（保持原始格式）")
    following: str = Field("0", description="关注数（保持原始格式）")


class XiaohongshuAccountCreate(XiaohongshuAccountBase):
    """创建小红书账号模式"""
    cookies: Optional[str] = Field(None, description="登录Cookie")
    is_current: bool = Field(False, description="是否为当前使用账号")


class XiaohongshuAccountUpdate(BaseModel):
    """更新小红书账号模式"""
    nickname: Optional[str] = Field(None, description="昵称")
    account_name: Optional[str] = Field(None, description="账号名")
    followers: Optional[str] = Field(None, description="粉丝数")
    following: Optional[str] = Field(None, description="关注数")
    cookies: Optional[str] = Field(None, description="登录Cookie")


class XiaohongshuAccount(XiaohongshuAccountBase):
    """小红书账号模式"""
    id: int = Field(..., description="账号ID")
    is_current: bool = Field(False, description="是否为当前使用账号")
    last_login: Optional[datetime] = Field(None, description="最后登录时间")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


# 登录相关模式
class LoginRequest(BaseModel):
    """登录请求模式"""
    auto_login: bool = Field(False, description="是否自动登录")


class LoginResponse(BaseModel):
    """登录响应模式"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="响应消息")
    account: Optional[XiaohongshuAccount] = Field(None, description="账号信息")
    token: Optional[str] = Field(None, description="访问令牌")
    qr_code_url: Optional[str] = Field(None, description="二维码图片URL")


class LoginStatusResponse(BaseModel):
    """登录状态响应模式"""
    success: bool = Field(..., description="是否成功")
    is_logged_in: bool = Field(..., description="是否已登录")
    message: str = Field(..., description="状态消息")
    account: Optional[XiaohongshuAccount] = Field(None, description="当前账号信息")


# 发布相关模式
class PublishRequest(BaseModel):
    """发布请求模式"""
    title: str = Field(..., min_length=1, max_length=200, description="标题")
    content: str = Field(..., min_length=1, description="内容")
    images: Optional[List[str]] = Field(None, description="图片列表")
    tags: Optional[str] = Field(None, description="标签")
    account_id: Optional[int] = Field(None, description="指定账号ID，不指定则使用当前账号")
    schedule_time: Optional[datetime] = Field(None, description="定时发布时间")


class PublishResponse(BaseModel):
    """发布响应模式"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="响应消息")
    log_id: Optional[int] = Field(None, description="发布日志ID")
    xiaohongshu_post_id: Optional[str] = Field(None, description="小红书帖子ID")
    xiaohongshu_url: Optional[str] = Field(None, description="小红书帖子URL")


# 发布日志相关模式
class XiaohongshuPublishLogBase(BaseModel):
    """发布日志基础模式"""
    title: str = Field(..., description="标题")
    content: str = Field(..., description="内容")
    images: Optional[List[str]] = Field(None, description="图片列表")
    tags: Optional[str] = Field(None, description="标签")


class XiaohongshuPublishLogCreate(XiaohongshuPublishLogBase):
    """创建发布日志模式"""
    account_id: int = Field(..., description="账号ID")
    post_id: int = Field(..., description="帖子ID")


class XiaohongshuPublishLog(XiaohongshuPublishLogBase):
    """发布日志模式"""
    id: int = Field(..., description="日志ID")
    account_id: int = Field(..., description="账号ID")
    post_id: int = Field(..., description="帖子ID")
    status: str = Field(..., description="发布状态")
    error_message: Optional[str] = Field(None, description="错误信息")
    xiaohongshu_post_id: Optional[str] = Field(None, description="小红书帖子ID")
    xiaohongshu_url: Optional[str] = Field(None, description="小红书帖子URL")
    publish_time: Optional[datetime] = Field(None, description="发布时间")
    views: int = Field(0, description="浏览量")
    likes: int = Field(0, description="点赞数")
    comments: int = Field(0, description="评论数")
    shares: int = Field(0, description="分享数")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


# Token相关模式
class XiaohongshuTokenBase(BaseModel):
    """Token基础模式"""
    token_type: str = Field(..., description="Token类型")
    token_value: str = Field(..., description="Token值")
    expires_at: Optional[datetime] = Field(None, description="过期时间")
    scope: Optional[str] = Field(None, description="权限范围")
    token_metadata: Optional[Dict[str, Any]] = Field(None, description="元数据")


class XiaohongshuTokenCreate(XiaohongshuTokenBase):
    """创建Token模式"""
    account_id: int = Field(..., description="账号ID")


class XiaohongshuToken(XiaohongshuTokenBase):
    """Token模式"""
    id: int = Field(..., description="Token ID")
    account_id: int = Field(..., description="账号ID")
    is_active: bool = Field(True, description="是否有效")
    last_used: Optional[datetime] = Field(None, description="最后使用时间")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


# 统计相关模式
class AccountStats(BaseModel):
    """账号统计模式"""
    followers: str = Field("0", description="粉丝数")
    following: str = Field("0", description="关注数")


class PublishStats(BaseModel):
    """发布统计模式"""
    total_posts: int = Field(0, description="总发布数")
    success_posts: int = Field(0, description="成功发布数")
    failed_posts: int = Field(0, description="失败发布数")
    pending_posts: int = Field(0, description="待发布数")
    total_views: int = Field(0, description="总浏览量")
    total_likes: int = Field(0, description="总点赞数")
    total_comments: int = Field(0, description="总评论数")
    total_shares: int = Field(0, description="总分享数")


# 响应模式
class AccountListResponse(BaseModel):
    """账号列表响应模式"""
    success: bool = Field(True, description="是否成功")
    accounts: List[XiaohongshuAccount] = Field(..., description="账号列表")
    total: int = Field(..., description="总数")


class PublishLogListResponse(BaseModel):
    """发布日志列表响应模式"""
    success: bool = Field(True, description="是否成功")
    logs: List[XiaohongshuPublishLog] = Field(..., description="发布日志列表")
    total: int = Field(..., description="总数")


class StatsResponse(BaseModel):
    """统计响应模式"""
    success: bool = Field(True, description="是否成功")
    account_stats: Optional[AccountStats] = Field(None, description="账号统计")
    publish_stats: Optional[PublishStats] = Field(None, description="发布统计")